// src/utils/visibilityControl.js
class VisibilityControl {
  constructor() {
    // 存储部件的可见性状态
    this.partStates = new Map();
  }

  // 隐藏指定部件
  hidePart(model, partName) {
    if (!model) return;

    model.traverse((child) => {
      if (
        child.isMesh &&
        child.name &&
        (child.name.toLowerCase().includes(partName.toLowerCase()) ||
          partName.toLowerCase().includes(child.name.toLowerCase()))
      ) {
        // 保存原始可见性状态
        if (!this.partStates.has(child.uuid)) {
          this.partStates.set(child.uuid, {
            originalVisible: child.visible,
            currentlyVisible: true,
          });
        }

        // 隐藏部件
        child.visible = false;

        // 更新状态
        const state = this.partStates.get(child.uuid);
        state.currentlyVisible = false;
        this.partStates.set(child.uuid, state);

        console.log(`部件 ${child.name} 已隐藏`);
      }
    });
  }

  // 显示指定部件
  showPart(model, partName) {
    if (!model) return;

    model.traverse((child) => {
      if (
        child.isMesh &&
        child.name &&
        (child.name.toLowerCase().includes(partName.toLowerCase()) ||
          partName.toLowerCase().includes(child.name.toLowerCase()))
      ) {
        // 显示部件
        child.visible = true;

        // 更新状态
        if (this.partStates.has(child.uuid)) {
          const state = this.partStates.get(child.uuid);
          state.currentlyVisible = true;
          this.partStates.set(child.uuid, state);
        }

        console.log(`部件 ${child.name} 已显示`);
      }
    });
  }

  // 切换部件可见性
  togglePartVisibility(model, partName) {
    if (!model) return;

    let isVisible = true;
    model.traverse((child) => {
      if (
        child.isMesh &&
        child.name &&
        (child.name.toLowerCase().includes(partName.toLowerCase()) ||
          partName.toLowerCase().includes(child.name.toLowerCase()))
      ) {
        if (this.partStates.has(child.uuid)) {
          const state = this.partStates.get(child.uuid);
          isVisible = state.currentlyVisible;
        } else {
          isVisible = child.visible;
        }
      }
    });

    if (isVisible) {
      this.hidePart(model, partName);
    } else {
      this.showPart(model, partName);
    }
  }

  // 隐藏多个部件
  hideMultipleParts(model, partNames) {
    if (!model) return;

    partNames.forEach((partName) => {
      this.hidePart(model, partName);
    });
  }

  // 显示多个部件
  showMultipleParts(model, partNames) {
    if (!model) return;

    partNames.forEach((partName) => {
      this.showPart(model, partName);
    });
  }

  // 隐藏所有部件
  hideAllParts(model) {
    if (!model) return;

    model.traverse((child) => {
      if (child.isMesh && child.name) {
        // 保存原始可见性状态
        if (!this.partStates.has(child.uuid)) {
          this.partStates.set(child.uuid, {
            originalVisible: child.visible,
            currentlyVisible: true,
          });
        }

        // 隐藏部件
        child.visible = false;

        // 更新状态
        const state = this.partStates.get(child.uuid);
        state.currentlyVisible = false;
        this.partStates.set(child.uuid, state);
      }
    });

    console.log("所有部件已隐藏");
  }

  // 显示所有部件
  showAllParts(model) {
    if (!model) return;

    model.traverse((child) => {
      if (child.isMesh) {
        // 显示部件
        child.visible = true;

        // 更新状态
        if (this.partStates.has(child.uuid)) {
          const state = this.partStates.get(child.uuid);
          state.currentlyVisible = true;
          this.partStates.set(child.uuid, state);
        }
      }
    });

    console.log("所有部件已显示");
  }

  // 恢复所有部件到原始可见性状态
  restoreAllPartsVisibility(model) {
    if (!model) return;

    model.traverse((child) => {
      if (child.isMesh && this.partStates.has(child.uuid)) {
        const state = this.partStates.get(child.uuid);
        child.visible = state.originalVisible;
      }
    });

    // 清空状态
    this.partStates.clear();
    console.log("所有部件可见性已恢复");
  }

  // 获取可见性控制实例
  static getInstance() {
    if (!this.instance) {
      this.instance = new VisibilityControl();
    }
    return this.instance;
  }
}

// 导出单例实例
export default VisibilityControl.getInstance();